/*
 * Copyright (c) 2024-2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "arkoala_api_generated.h"

#include "core/components_ng/base/frame_node.h"
#include "core/components_ng/pattern/container_picker/container_picker_model_static.h"
#include "core/interfaces/native/utility/callback_helper.h"
#include "core/interfaces/native/utility/converter.h"
#include "core/interfaces/native/utility/reverse_converter.h"

namespace OHOS::Ace::NG::GeneratedModifier {
namespace PickerModifier {
Ark_NativePointer ConstructImpl(Ark_Int32 id, Ark_Int32 flags)
{
    auto frameNode = ContainerPickerModelStatic::CreateFrameNode(id);
    CHECK_NULL_RETURN(frameNode, nullptr);
    frameNode->IncRefCount();
    return AceType::RawPtr(frameNode);
}
} // namespace PickerModifier
namespace PickerInterfaceModifier {
void SetPickerOptionsImpl(Ark_NativePointer node, const Opt_PickerOptions* options)
{
    auto frameNode = reinterpret_cast<FrameNode*>(node);
    CHECK_NULL_VOID(frameNode);
    if (options && options->tag != INTEROP_TAG_UNDEFINED) {
        auto optSelected = options->value.selectedIndex;
        if (optSelected.tag != INTEROP_TAG_UNDEFINED) {
            auto arkValue = Converter::Convert<int32_t>(optSelected.value);
            ContainerPickerModelStatic::SetSelectedIndex(frameNode, arkValue);
        }
    }
}
} // namespace PickerInterfaceModifier
namespace PickerAttributeModifier {
void SetOnChangeImpl(Ark_NativePointer node, const Opt_OnPickerCallback* value)
{
    auto frameNode = reinterpret_cast<FrameNode*>(node);
    CHECK_NULL_VOID(frameNode);
    auto optCallback = Converter::GetOptPtr(value);
    if (!optCallback) {
        return;
    }
    auto onEvent = [arkCallback = CallbackHelper(*optCallback)](const double& param) {
        auto arkValue = Converter::ArkValue<Ark_Int32>(static_cast<int32_t>(param));
        arkCallback.Invoke(arkValue);
    };
    ContainerPickerModelStatic::SetOnChange(frameNode, std::move(onEvent));
}

void SetOnScrollStopImpl(Ark_NativePointer node, const Opt_OnPickerCallback* value)
{
    auto frameNode = reinterpret_cast<FrameNode*>(node);
    CHECK_NULL_VOID(frameNode);
    auto optCallback = Converter::GetOptPtr(value);
    if (!optCallback) {
        return;
    }
    auto onEvent = [arkCallback = CallbackHelper(*optCallback)](const double& param) {
        auto arkValue = Converter::ArkValue<Ark_Int32>(static_cast<int32_t>(param));
        arkCallback.Invoke(arkValue);
    };
    ContainerPickerModelStatic::SetOnScrollStop(frameNode, std::move(onEvent));
}

void SetCanLoopImpl(Ark_NativePointer node, const Opt_Boolean* value)
{
    auto frameNode = reinterpret_cast<FrameNode*>(node);
    CHECK_NULL_VOID(frameNode);
    const auto convValue = Converter::OptConvertPtr<bool>(value);
    if (convValue) {
        ContainerPickerModelStatic::SetCanLoop(frameNode, *convValue);
        return;
    }
    ContainerPickerModelStatic::SetCanLoop(frameNode, true);
}

void SetEnableHapticFeedbackImpl(Ark_NativePointer node, const Opt_Boolean* value)
{
    auto frameNode = reinterpret_cast<FrameNode*>(node);
    CHECK_NULL_VOID(frameNode);
    const auto convValue = Converter::OptConvertPtr<bool>(value);
    if (convValue) {
        ContainerPickerModelStatic::SetEnableHapticFeedback(frameNode, *convValue);
        return;
    }
    ContainerPickerModelStatic::SetEnableHapticFeedback(frameNode, true);
}

void SetSelectionIndicatorImpl(Ark_NativePointer node, const Opt_PickerIndicatorStyle* value)
{
    auto frameNode = reinterpret_cast<FrameNode*>(node);
    CHECK_NULL_VOID(frameNode);
    auto pickerIndicatorStyle = Converter::OptConvertPtr<PickerIndicatorStyle>(value);
    if (pickerIndicatorStyle) {
        ContainerPickerModelStatic::SetIndicatorStyle(frameNode, pickerIndicatorStyle.value());
    }
}
} // namespace PickerAttributeModifier
const GENERATED_ArkUIPickerModifier* GetPickerModifier()
{
    static const GENERATED_ArkUIPickerModifier ArkUIPickerModifierImpl {
        PickerModifier::ConstructImpl,
        PickerInterfaceModifier::SetPickerOptionsImpl,
        PickerAttributeModifier::SetOnChangeImpl,
        PickerAttributeModifier::SetOnScrollStopImpl,
        PickerAttributeModifier::SetCanLoopImpl,
        PickerAttributeModifier::SetEnableHapticFeedbackImpl,
        PickerAttributeModifier::SetSelectionIndicatorImpl,
    };
    return &ArkUIPickerModifierImpl;
}

} // namespace OHOS::Ace::NG::GeneratedModifier
